<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight 1.6.3
by Lorenzo Bettini, bettini@gnu.org
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>code/Ch2/mail/MessageQueue.java</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<pre>
<tt>
01: <b><font color=#000080>import</font></b> java<font color=#990000>.</font>util<font color=#990000>.</font>ArrayList<font color=#990000>;</font>
02: 
03: <i><font color=#9A1900>/**</font></i>
04: <i><font color=#9A1900>   A first-in, first-out collection of messages. This</font></i>
05: <i><font color=#9A1900>   implementation is not very efficient. We will consider</font></i>
06: <i><font color=#9A1900>   a more efficient implementation in chapter 3.</font></i>
07: <i><font color=#9A1900>*/</font></i>
08: <b><font color=#0000FF>public</font></b> <b><font color=#0000FF>class</font></b> MessageQueue
09: <font color=#FF0000>{</font>
10:    <i><font color=#9A1900>/**</font></i>
11: <i><font color=#9A1900>      Constructs an empty message queue.</font></i>
12: <i><font color=#9A1900>   */</font></i>
13:    <b><font color=#0000FF>public</font></b> <b><font color=#000000>MessageQueue</font></b><font color=#990000>(</font><font color=#990000>)</font>
14:    <font color=#FF0000>{</font>
15:       queue <font color=#990000>=</font> <b><font color=#0000FF>new</font></b> <b><font color=#000000>ArrayList</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>
16:    <font color=#FF0000>}</font>
17: 
18:    <i><font color=#9A1900>/**</font></i>
19: <i><font color=#9A1900>      Remove message at head.</font></i>
20: <i><font color=#9A1900>      @return message that has been removed from the queue</font></i>
21: <i><font color=#9A1900>   */</font></i>
22:    <b><font color=#0000FF>public</font></b> Message <b><font color=#000000>removeFirst</font></b><font color=#990000>(</font><font color=#990000>)</font>
23:    <font color=#FF0000>{</font>
24:       <b><font color=#0000FF>return</font></b> <font color=#990000>(</font>Message<font color=#990000>)</font> queue<font color=#990000>.</font><b><font color=#000000>remove</font></b><font color=#990000>(</font><font color=#993399>0</font><font color=#990000>)</font><font color=#990000>;</font>
25:    <font color=#FF0000>}</font>
26: 
27:    <i><font color=#9A1900>/**</font></i>
28: <i><font color=#9A1900>      Append message at tail.</font></i>
29: <i><font color=#9A1900>      @param newMessage the message to be appended</font></i>
30: <i><font color=#9A1900>   */</font></i>
31:    <b><font color=#0000FF>public</font></b> <font color=#009900>void</font> <b><font color=#000000>add</font></b><font color=#990000>(</font>Message newMessage<font color=#990000>)</font>
32:    <font color=#FF0000>{</font>
33:       queue<font color=#990000>.</font><b><font color=#000000>add</font></b><font color=#990000>(</font>newMessage<font color=#990000>)</font><font color=#990000>;</font>
34:    <font color=#FF0000>}</font>
35: 
36:    <i><font color=#9A1900>/**</font></i>
37: <i><font color=#9A1900>      Get the total number of messages in the queue.</font></i>
38: <i><font color=#9A1900>      @return the total number of messages in the queue</font></i>
39: <i><font color=#9A1900>   */</font></i>
40:    <b><font color=#0000FF>public</font></b> <font color=#009900>int</font> <b><font color=#000000>size</font></b><font color=#990000>(</font><font color=#990000>)</font>
41:    <font color=#FF0000>{</font>
42:       <b><font color=#0000FF>return</font></b> queue<font color=#990000>.</font><b><font color=#000000>size</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>
43:    <font color=#FF0000>}</font>
44: 
45:    <i><font color=#9A1900>/**</font></i>
46: <i><font color=#9A1900>      Get message at head.</font></i>
47: <i><font color=#9A1900>      @return message that is at the head of the queue</font></i>
48: <i><font color=#9A1900>   */</font></i>
49:    <b><font color=#0000FF>public</font></b> Message <b><font color=#000000>getFirst</font></b><font color=#990000>(</font><font color=#990000>)</font>
50:    <font color=#FF0000>{</font>
51:       <b><font color=#0000FF>return</font></b> <font color=#990000>(</font>Message<font color=#990000>)</font> queue<font color=#990000>.</font><b><font color=#000000>get</font></b><font color=#990000>(</font><font color=#993399>0</font><font color=#990000>)</font><font color=#990000>;</font>
52:    <font color=#FF0000>}</font>
53: 
54:    <b><font color=#0000FF>private</font></b> ArrayList queue<font color=#990000>;</font>
55: <font color=#FF0000>}</font>
</tt>
</pre>
</body>
</html>
